<?php


namespace app\home\severs;


use think\Exception;
use think\facade\Db;

class SaleContractDetailSever
{
    /**
     * @param string $id
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @autho:xiangzi
     * @auth true
     * @Date:2025/11/11
     * 改变锁定状态
     */
    public static function changeLockStatus($ids='')
    {
        if (is_array($ids)){
            foreach (array_unique($ids) as $id){
                $info=Db::name('sale_contract_detail')->where('id',$id)->find();
                $lockNum=Db::name('sale_contract_lock')->where('detailId',$id)->where('status','>',-1)
                    ->sum('lockNum');
                $baseNum=$info['bjNum']??0;
                $upData=['update_time'=>time()];
                if ($lockNum==0){
                    $upData['lockStatus']=1;
                }elseif($baseNum<=$lockNum){
                    $upData['lockStatus']=3;
                }elseif($baseNum>$lockNum){
                    $upData['lockStatus']=2;
                }
                try {
                    Db::name('sale_contract_detail')->where('id',$id)->update($upData);
                }catch (\Exception $e){
                    throw new Exception($e->getMessage());
                }
            }
        }else{
            $id=$ids;
        }
        $info=Db::name('sale_contract_detail')->where('id',$id)->find();
        $lockNum=Db::name('sale_contract_lock')->where('detailId',$id)->where('status','>',-1)
            ->sum('lockNum');
        $baseNum=$info['bjNum']??0;
        $upData=['update_time'=>time()];
        if ($lockNum==0){
            $upData['lockStatus']=1;
        }elseif($baseNum<=$lockNum){
            $upData['lockStatus']=3;
        }elseif($baseNum>$lockNum){
            $upData['lockStatus']=2;
        }
        try {
            Db::name('sale_contract_detail')->where('id',$id)->update($upData); 
        }catch (\Exception $e){
            throw new Exception($e->getMessage());
        }
        return;
        
       
}

    /**
     * @param string $id
     * @autho:xiangzi
     * @auth true
     * @Date:2025/11/11
     * 改变采购状态
     */
    public function changeBuyStatus($id='')
    {
        $info=Db::name('sale_contract_detail')->where('id',$id)->find();
}
}